<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>20210105</title>
    <style>
        .container {
            width: 860px;
            margin: 10px auto;
        }
        .products {
            display: flex;
            flex-wrap: wrap;
            flex-direction: row;
        }
        .item {
            border: 1px solid black;
            width: 200px;
            height: 200px;
            margin: 5px;
        }
        .item > .img {
            height: 150px;
            overflow: hidden;
        }
        .img > img {
            width: 100%;
        }
        .item .desc {
            padding: 5px;
        }
        .item .ac:after {
            content: '';
            clear: both;
            display: block;
        }
        .item .ac .p {
            float: left;
        }
        .item .ac .add {
            float: right;
        }
        .page {
            margin-top: 10px;
            text-align: center;
        }
        .page > a {
            font-size: 24px;
            background-color: #00c0ff;
            color: white;
            padding: 10px;
            text-decoration: none;
            margin: 0 5px;
        }
    </style>
    <script src="./jquery1.9/jquery-1.9.0.min.js"></script>
    <script>
        let pageNum = 1;
        function loadData(pageNo) {
            pageNum = pageNo;
            let params = $("#searchForm").serializeArray();
            params.push({name:"pageNum", value:pageNo});
            console.log(params);
            $.post("./service/selectProductByPage.php", params, (result)=>{
                console.log(result);
                initData(result);

                // 处理分页按钮

                let btnTxt = "";
                $(".page").empty();
                if(result.pageNum > 1){
                    btnTxt += `<a href="javascript:doPage(-1);">上一页</a>`;
                }
                btnTxt += `<a href="javascript:doPage(0);">${result.pageNum}</a>`;
                if(result.pageNum < result.totalPage){
                    btnTxt += `<a href="javascript:doPage(1);">下一页</a>`;
                }
                $(".page").append(btnTxt);
            }, "json");
        }
        function initData(result) {
            // 数据的展示
            $(".products").empty();// 清空之前的数据
            for(let p of result.list) {
                let txt = `<div class="item" >
                    <div class="img" >
                        <img src="${p.p_image_url}" alt="no image">
                    </div>
                    <div class="desc" >
                        <div><a href="javascript:;">${p.p_name}</a></div>
                        <div class="ac" >
                            <span class="p" >${p.p_price}</span>
                            <a href="javascript:;" class="add" >加入购物车</a>
                        </div>
                    </div>
                </div>`;
                $(".products").append(txt);
            }
            if(result.list.length == 0){
                // 没有查询到数据
                $(".products").append(`<div class="item" >未查询到相关数据</div>`);
            }
        }
        function doPage(type) {
            loadData(pageNum + type);
            console.log("pageNum=", pageNum);
        }

        $(function(){
            // 初始数据的显示
            loadData(1);
        });
    </script>
</head>
<body>
    <div class="container" >
        <div>
            <form id="searchForm" >
                <div>
                    <label>商品编号:<input type="text" name="pid" ></label>
                    <label>商品名称:<input type="text" name="pname" ></label>
                </div>
                <div>
                    <label>商品价格:<input type="text" name="begin" > ~ <input type="text" name="end" ></label>
                </div>
                <div>
                    <input type="button" value="search" onclick="loadData(1)" />
                </div>
            </form>
        </div>
        <div class="products" >

            <div class="item" >
                <div class="img" >
                    <img src="" alt="no image">
                </div>
                <div class="desc" >
                    <div><a href="javascript:;">p_name</a></div>
                    <div class="ac" >
                        <span class="p" >p_price</span>
                        <a href="javascript:;" class="add" >加入购物车</a>
                    </div>
                </div>
            </div>

        </div>

        <div class="page" >
            
        </div>
    </div>
</body>
</html>